package com.rules.admin.expression;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

import java.util.List;

public class IfExpression implements Expression {
   private Expression conditionExpression;
   private List<ElseIfExpression> elseIfExpressions = Lists.newArrayList();
   private ElseExpression elseExpression;
   private List<Expression> executeExpressions = Lists.newArrayList();

   public void accept(ExpressionVisitor visitor) {
      visitor.vistor(this);
   }

   public Expression getConditionExpression() {
      return this.conditionExpression;
   }

   public void setConditionExpression(Expression conditionExpression) {
      this.conditionExpression = conditionExpression;
   }

   public List<ElseIfExpression> getElseIfExpressions() {
      return this.elseIfExpressions;
   }

   public void setElseIfExpressions(List<ElseIfExpression> elseIfExpressions) {
      this.elseIfExpressions = elseIfExpressions;
   }

   public ElseExpression getElseExpression() {
      return this.elseExpression;
   }

   public void setElseExpression(ElseExpression elseExpression) {
      this.elseExpression = elseExpression;
   }

   public List<Expression> getExecuteExpressions() {
      return this.executeExpressions;
   }

   public void setExecuteExpressions(List<Expression> executeExpressions) {
      this.executeExpressions = executeExpressions;
   }

   public void addExecuteExpression(Expression expression) {
      this.executeExpressions.add(expression);
   }

   public void addElseIfExpression(ElseIfExpression expression) {
      this.elseIfExpressions.add(expression);
   }

   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("if(").append(this.conditionExpression.toString()).append("){").append("\n\t").append(Joiner.on(";\n").join(this.executeExpressions).replaceAll("\\n", "\\\n\t")).append("\n}");
      if (!this.elseIfExpressions.isEmpty()) {
         builder.append(Joiner.on(";\n").join(this.elseIfExpressions));
      }

      if (this.elseExpression != null) {
         builder.append(this.elseExpression);
      }

      return builder.toString();
   }
}
